package com.homson.servicedomain.domain.transaction;

import com.homson.coreservice.servicecommon.expection.SystemException;
import com.homson.servicedomain.domain.account.AccountQueryCommand;
import com.homson.coreservice.serviceinfrastructure.converter.TransactionConverter;
import com.homson.coreservice.serviceinfrastructure.datasource.model.account.TransactionDO;
import com.homson.coreservice.serviceinfrastructure.datasource.repository.account.TransactionRepository;
import com.homson.coreservice.serviceinfrastructure.dto.TransactionDto;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author: homson_leo
 * @contact: hey_超级巨星（Homson_Leo）- 刘泓岑 1196816884@qq.com
 * @time: 2025/10/22 11:12 PM
 **/
@Service
@Slf4j
public class TransactionQueryCommand {
    
    @Resource
    private TransactionRepository transactionRepository;
    @Resource
    private AccountQueryCommand accountQueryCommand;
    
    public List<TransactionDto> queryRecentTransactionList(Long userId, Integer pageNum, Integer pageSize) throws Exception{
        try {
            List<Long> accountIds = accountQueryCommand.queryAllAccountIdList(userId);
            if (CollectionUtils.isEmpty(accountIds)) {
                return Collections.emptyList();
            }
            List<TransactionDO> transactionDOS = transactionRepository.findUserRecentTransactionByPage(accountIds, pageNum, pageSize);
            if (CollectionUtils.isEmpty(transactionDOS)) {
                return Collections.emptyList();
            }
            List<TransactionDto> transactionDtoList = TransactionConverter.convert(transactionDOS);
            return transactionDtoList;
        } catch (Exception e) {
            log.error("查询用户最近账本明细列表异常", e.getStackTrace());
            throw new SystemException(e.getMessage());
        }
    }
    
    
}
